package com.bw.merchant.net

import androidx.lifecycle.MutableLiveData
import com.bw.common.vm.BaseViewModel
import com.bw.http.utils.zgr.OkHttpClientManager
import com.bw.merchant.entity.MerchantEntity
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import retrofit2.http.Query
import java.io.File

/**
 * Author :
 * Created by lhy on 2025/5/8.
 * Description:
 */
class SettledViewModel : BaseViewModel() {
    private val apiService = OkHttpClientManager.retrofitInstance.create(ApiService::class.java)
    val merchantSuccess by lazy { MutableLiveData<List<MerchantEntity>>() }
    val postMerchantSuccess by lazy { MutableLiveData<MerchantEntity>() }
    val uploadFileSuccess by lazy { MutableLiveData<String>() }

    val serialNumberSuccess by lazy { MutableLiveData<String>() }

    fun getMerchant() {
        executeNetwork(
            { apiService.getMerchant() },
            merchantSuccess::postValue
        )
    }

    fun postMerchant(merchantEntity: MerchantEntity) {
        executeNetwork(
            { apiService.postMerchant(merchantEntity) },
            postMerchantSuccess::postValue
        )
    }

    fun uploadFile(
        file:File,
        modulename: String,
        userid: String
    ) {

        val body = RequestBody.create("multipart/form-data".toMediaTypeOrNull(), file)
        val filePart = MultipartBody.Part.createFormData("file", file.name, body)

        executeNetwork(
            { apiService.uploadFile(filePart,modulename, userid) },
            uploadFileSuccess::postValue
        )
    }

    fun getSerialNumber(prefix:String = "RZ"){
        executeNetwork(
            { apiService.getSerialNumber(prefix) },
            serialNumberSuccess::postValue
        )
    }

}